#ifndef _POLLMGR_H
#define _POLLMGR_H

#include "poller.h"

//since pthread_once can not invoke function with parameter.
//i dont know how to invoke Constructor with parameter right now:-..
#define MAX_POLLER_SIZE 1000

//interface for obj can be polled
class Pollable {
	public:
		//@fd: fd for read
		virtual int HandleRead(int fd)  = 0;

		//@fd: fd for write
		virtual int HandleWrite(int fd) = 0;

		virtual ~Pollable() { }
};

class PollMgr {
	public:
		static PollMgr *Instance();
		static void PollMgrInit();
		~PollMgr();

		bool AddPollable(int fd, PollFlag flag, Pollable *pb);
		bool DelPollable(int fd, PollFlag flag, Pollable *pb);
		void WaitLoop(int timeout = -1);

	private:
		PollMgr(int max_callback = 0);
		PollMgr(const PollMgr &);

	private:
		static PollMgr *_instance;
		pthread_mutex_t _m;
		Poller *_poller;
		Pollable **_pollables;
};
#endif //pollmgr.h
